package com.wu.cloud.userservice.controller;

import com.wu.cloud.userservice.dao.UserAccountMapperDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author xwu36
 */
@Controller
@Slf4j
@SessionAttributes("authorizationRequest")
public class OAuthApprovalController {

    @Autowired
    UserAccountMapperDao userAccountMapperDao;

    @RequestMapping("/oauth/custom/confirm_access")
    public ModelAndView getAccessConfirmation(Map<String, Object> model, HttpServletRequest request) throws Exception {
        AuthorizationRequest authorizationRequest = (AuthorizationRequest) model.get("authorizationRequest");
        Set<String> scope = authorizationRequest.getScope();
        String role = scope.iterator().next();
        List<Map<String, String>> permissions = userAccountMapperDao.getPermissionByRole(role);
        ModelAndView view = new ModelAndView();
        view.setViewName("pesk_comfirm");
        view.addObject("clientId", authorizationRequest.getClientId());
        view.addObject("permissionList",permissions);
        return view;
    }
}
